package com.fhz.day014;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    Intent intent;
    MyService.MyBinder binder;
    ServiceConnection connection;
    private static final String TAG = "feng";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void start(View view) {
        intent = new Intent(this,MyService.class);
        startService(intent);
    }

    public void stop(View view) {
        stopService(intent);
    }


    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void bind(View view) {
//        Intent intent = new Intent(this, MyService.class);
         connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                binder  = (MyService.MyBinder) service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.i(TAG, "onServiceDisconnected: +服务异常停止的时候,才会调用");
            }
        };
        bindService(intent,connection,BIND_AUTO_CREATE);

    }

    public void invoke(View view) {
        binder.callShow1();
    }

    public void unbind(View view) {
        unbindService(connection);
    }
}